/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tces.view;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tces.controller.CollegeDAO;
import com.tces.controller.InstructorDAO;
import com.tces.model.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author Arnold
 */
public class InstructorAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(InstructorAction.class);
    private User user;
    private String oper;
    private List<Instructor> instructorList;
    private List<Instructor> nameList = new ArrayList<Instructor>();
    private String id, instructorIDNo, instructorFirstName, instructorMiddleName,
            instructorLastName, instructorVisible, collegeCode;

    @Override
    public String execute() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
             if (user.getRoleName().equalsIgnoreCase("Administrator") || user.getRoleName().equalsIgnoreCase("Guidance Counseling Head")) {
                nameList = InstructorDAO.getFullname();
            } else {
                nameList = InstructorDAO.getFullname(user.getCollegeID());
            }
            
        } catch (Exception ex) {
            log.error(ex);
        }
        return SUCCESS;
    }

    public String view() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");
            instructorList = InstructorDAO.getInstructors();
            log.info("INSTRUCTOR LIST SIZE: " + instructorList.size());
        } catch (Exception ex) {
            log.error(ex);

        }
        return SUCCESS;
    }

    public String edit() {
        try {
            user = (User) ActionContext.getContext().getSession().get("user");

            if (oper.equalsIgnoreCase("add")) {

                if (InstructorDAO.add(getInstructor()) > 0) {
                    addActionMessage("Successfull insert of Subject (" + getInstructor().getInstructorIDNo() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("edit")) {
                if (InstructorDAO.update(getInstructor()) > 0) {
                    addActionMessage("Successfull update of instructor(" + getInstructor().getInstructorIDNo() + ")");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else if (oper.equalsIgnoreCase("del")) {
                if (InstructorDAO.delete(Integer.parseInt(id)) > 0) {
                    addActionMessage("Successfull deactivation of instructor[ID:" + id + "]");
                    return SUCCESS;
                } else {
                    addActionError("An error occured. Please contact your System Administrator");
                    return ERROR;
                }
            } else {
                addActionMessage("Invalid operation!");
                return ERROR;
            }
        } catch (Exception ex) {
            addActionError(ex.toString());
            log.error(ex);
            return ERROR;
        }
    }

    private Instructor getInstructor() {
        Instructor instructor = new Instructor();
        if (instructorIDNo != null) {
            instructor.setInstructorIDNo(instructorIDNo);
        }
        if (instructorFirstName != null) {
            instructor.setInstructorFirstName(instructorFirstName);
        }
        if (instructorMiddleName != null) {
            instructor.setInstructorMiddleName(instructorMiddleName);
        }
        if (instructorLastName != null) {
            instructor.setInstructorLastName(instructorLastName);
        }
        if (instructorVisible != null) {
            instructor.setInstructorVisible(Integer.valueOf(instructorVisible));
        }
        if (collegeCode != null) {
            instructor.setCollegeID(Integer.valueOf(collegeCode));
        }

        if (id != null && !(id.equalsIgnoreCase("_empty"))) {
            log.info("" + id.equalsIgnoreCase("_empty"));
            instructor.setInstructorID(Integer.parseInt(id));
        }

        return instructor;
    }

    public String getCollegeCode() {
        return collegeCode;
    }

    public void setCollegeCode(String collegeCode) {
        this.collegeCode = collegeCode;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getInstructorFirstName() {
        return instructorFirstName;
    }

    public void setInstructorFirstName(String instructorFirstName) {
        this.instructorFirstName = instructorFirstName;
    }

    public String getInstructorIDNo() {
        return instructorIDNo;
    }

    public void setInstructorIDNo(String instructorIDNo) {
        this.instructorIDNo = instructorIDNo;
    }

    public String getInstructorLastName() {
        return instructorLastName;
    }

    public void setInstructorLastName(String instructorLastName) {
        this.instructorLastName = instructorLastName;
    }

    public List<Instructor> getInstructorList() {
        return instructorList;
    }

    public void setInstructorList(List<Instructor> instructorList) {
        this.instructorList = instructorList;
    }

    public String getInstructorMiddleName() {
        return instructorMiddleName;
    }

    public void setInstructorMiddleName(String instructorMiddleName) {
        this.instructorMiddleName = instructorMiddleName;
    }

    public String getInstructorVisible() {
        return instructorVisible;
    }

    public void setInstructorVisible(String instructorVisible) {
        this.instructorVisible = instructorVisible;
    }

    public String getOper() {
        return oper;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public List<Instructor> getNameList() {
        return nameList;
    }

    public void setNameList(List<Instructor> nameList) {
        this.nameList = nameList;
    }

   
}
